/* 
 * [New BSD License (BSD 3-Clause License)]
 * Copyright (c) 2012, Max Bechtold
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     - Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     - Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     - Neither the name of the Rel2Xml Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package maxbe.rel2xml.generator.dml;

import java.util.ArrayList;
import java.util.List;

import maxbe.rel2xml.generator.dml.predicates.ColumnValueComparison;
import maxbe.rel2xml.generator.dml.predicates.Conjunction;
import maxbe.rel2xml.generator.dml.predicates.Disjunction;
import maxbe.rel2xml.generator.dml.predicates.Predicate;

public abstract class AbstractDmlInstance implements DmlInstance {

	/** Name of this DmlInstance */
	protected String name;

	/**
	 * The predicate of this DmlInstance. This can be <i>null</i>, and is always
	 * <i>null</i> for Inserts
	 */
	protected Conjunction predicate;

	/** Path to the root node of the document */
	protected String rootNodePath;

	@Override
	public String getName() {
		return name;
	}

	public Predicate getPredicate() {
		return predicate;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	public void setPredicate(Conjunction predicate) {
		this.predicate = predicate;
	}

	public void setRootNodePath(String rootNodePath) {
		this.rootNodePath = rootNodePath;
	}

	protected String declareExtVars() {
		return declareExtVars(new ArrayList<String>());
	}

	protected String declareExtVars(List<String> variables) {
		if (predicate == null)
			return "";

		String varDecl = "";
		for (Predicate pred : predicate.getPredicates()) {
			if (pred instanceof ColumnValueComparison
					&& ((ColumnValueComparison) pred).getValue().equals("?")) {
				ColumnValueComparison cv = (ColumnValueComparison) pred;
				String var = getVar(cv.getColumn().getName(), variables);
				cv.setValue('$' + var);
				varDecl += String.format("declare variable $%s external;\n",
						var);
			} else if (pred instanceof Disjunction) {
				for (Predicate pred2 : ((Disjunction) pred).getPredicates())
					if (pred2 instanceof ColumnValueComparison
							&& ((ColumnValueComparison) pred2).getValue()
									.equals("?")) {
						ColumnValueComparison cv = (ColumnValueComparison) pred2;
						String var = getVar(cv.getColumn().getName(), variables);
						cv.setValue('$' + var);
						varDecl += String.format(
								"declare variable $%s external;\n", var);
					}
			}
		}
		return varDecl;
	}

	protected String getVar(String name, List<String> variables) {
		String var = name;
		int i = 2;
		while (variables.contains(var))
			var = name + i++;
		variables.add(var);
		return var;
	}

	@Override
	public String toXQuery() {
		String xquery = "";
		if (name != null)
			xquery += String.format("(: %s :)\n\n", name);

		return xquery;
	}

}